﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyKho.Server;

namespace QuanLyKho.Server.BLL
{
    public class Dm_HamLuongBLL
    {
        public Dm_HamLuongBLL()
        {
        }
        public Dm_HamLuong AddNewDmHamLuong(Dm_HamLuong objectDmHamLuong)
        {
            Dm_HamLuong dmHLTemple = new Dm_HamLuong();
            int error = -1;
            try
            {
                dmHLTemple = objectDmHamLuong;
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    entities.Dm_HamLuong.Add(dmHLTemple);
                    error = entities.SaveChanges();
                    if (error < 0)
                    {
                        dmHLTemple = null;
                    }
                }
            }
            catch
            {
            }
            return dmHLTemple;
        }

        public bool UpdateDmHamLuong(Dm_HamLuong objectDmHamLuong)
        {
            Dm_HamLuong dmHLTemple = new Dm_HamLuong();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmHLTemple = entities.Dm_HamLuong.SingleOrDefault(c => c.MaHamLuong == objectDmHamLuong.MaHamLuong);
                    dmHLTemple.TenHamLuong = objectDmHamLuong.TenHamLuong;
                    dmHLTemple.KichHoat = objectDmHamLuong.KichHoat;
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public bool DeleteDmHamLuong(Dm_HamLuong objectDmHamLuong)
        {
            Dm_HamLuong dmHLTemple = new Dm_HamLuong();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmHLTemple = entities.Dm_HamLuong.SingleOrDefault(c => c.MaHamLuong == objectDmHamLuong.MaHamLuong);
                    entities.Dm_HamLuong.Remove(dmHLTemple);
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public List<Dm_HamLuong> ListDmHamLuong()
        {
            List<Dm_HamLuong> dmHLTemple = new List<Dm_HamLuong>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmHLTemple = (from c in entities.Dm_HamLuong select c).ToList();
                }
            }
            catch
            {
            }
            return dmHLTemple;
        }

        public List<Dm_HamLuong> FindListDmHamLuong(string maHL, string tenHL)
        {
            List<Dm_HamLuong> dmHLTemple = new List<Dm_HamLuong>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    if (!string.IsNullOrEmpty(maHL))
                    {
                        dmHLTemple = (from c in entities.Dm_HamLuong
                                       where c.MaHamLuong.Contains(maHL)
                                       select c).ToList();
                    }
                    else if (!string.IsNullOrEmpty(tenHL))
                    {
                        dmHLTemple = (from c in entities.Dm_HamLuong
                                       where c.MaHamLuong.Contains(tenHL)
                                       select c).ToList();
                    }
                    else
                    {
                        dmHLTemple = (from c in entities.Dm_HamLuong
                                       where c.MaHamLuong.Contains(maHL)
                                             && c.MaHamLuong.Contains(tenHL)
                                       select c).ToList();
                    }
                }
            }
            catch
            {
            }
            return dmHLTemple;
        }

        public bool FindTrungMaDmHamLuong(string maHL)
        {
            Dm_HamLuong dmHLTemple = new Dm_HamLuong();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmHLTemple = entities.Dm_HamLuong.SingleOrDefault(c => c.MaHamLuong == maHL);
                    if (dmHLTemple != null)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }
    }
}
